package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.actions.watcher.ChooseOneAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.BarricadePower;
import com.megacrit.cardcrawl.rooms.MonsterRoom;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.cards.optionCards.ChooseMonsterCard;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.powers.TauntPower;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Countermeasure extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("Countermeasure");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Countermeasure.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = -2;

    public Countermeasure() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 10;
        this.tags.add(CardTagsEnum.VIRUS);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if (!upgraded) {
            m = AbstractDungeon.getRandomMonster();
        }
        if (m != null) {
            addToBot(new GainBlockAction(m, m, this.magicNumber));
            addToBot(new ApplyPowerAction(m, m, new BarricadePower(m)));
        }
        addToTop(new AbstractGameAction() {
            @Override
            public void update() {
                if (upgraded) {
                    List<AbstractMonster> aliveMonsters = AbstractDungeon.getMonsters().monsters.stream().filter(monster -> !monster.isDeadOrEscaped()).collect(Collectors.toList());
                    if (aliveMonsters.size() >= 2) {
                        ArrayList<AbstractCard> options = new ArrayList<>();
                        for (AbstractMonster monster : aliveMonsters) {
                            AbstractCard option = new ChooseMonsterCard(monster, () -> {
                                addToTop(new ApplyPowerAction(monster, monster, new BarricadePower(monster)));
                                addToTop(new ApplyPowerAction(monster, monster, new TauntPower(monster)));
                                if (monster.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                                    addToTop(new GainBlockAction(monster, monster, magicNumber * 2));
                                } else {
                                    addToTop(new GainBlockAction(monster, monster, magicNumber));
                                }
                            });
                            options.add(option);
                        }
                        addToTop(new ChooseOneAction(options));
                    } else if (!aliveMonsters.isEmpty()) {
                        AbstractMonster m = aliveMonsters.get(0);
                        addToTop(new ApplyPowerAction(m, m, new TauntPower(m)));
                        addToTop(new ApplyPowerAction(m, m, new BarricadePower(m)));
                        if (m.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                            addToTop(new GainBlockAction(m, m, magicNumber * 2));
                        } else {
                            addToTop(new GainBlockAction(m, m, magicNumber));
                        }
                    }
                }
                isDone = true;
            }
        });

    }
}
